//
//  AppDelegate.swift
//  BaseProj
//
//  Created by 陈宇 on 2017/4/26.
//  Copyright © 2017年 科匠武汉. All rights reserved.
//

import UIKit
import RxSwift
import GoogleMaps
import IQKeyboardManagerSwift
import SlideMenuControllerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        configureLog()
        
        configureSlideMenuOptions()
        
        configureIQKeyboardManager()
        
        GMSServices.provideAPIKey("AIzaSyAICApQUx2DUnyp3CFQUxmhPMza1INAYhA")
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.makeKeyAndVisible()
        
        let menu = MenuViewController()
        self.window?.rootViewController = SlideMenuController(mainViewController: menu.menuItems.first!.1, leftMenuViewController: menu)
        
        return true
    }
    
    fileprivate func configureSlideMenuOptions() {
        SlideMenuOptions.contentViewOpacity = 0.4
        SlideMenuOptions.hideStatusBar = false
    }
    
}


extension SlideMenuController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return self.mainViewController?.preferredStatusBarStyle ?? .lightContent
    }
}

extension AppDelegate {
    func configureIQKeyboardManager() {
        IQKeyboardManager.sharedManager().enable = true
        IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true
        IQKeyboardManager.sharedManager().enableAutoToolbar = false
        IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = 60
        IQKeyboardManager.sharedManager().registerTextFieldViewClass(SecurePayInputView.self, didBeginEditingNotificationName: NSNotification.Name.UITextFieldTextDidBeginEditing.rawValue, didEndEditingNotificationName: NSNotification.Name.UITextFieldTextDidEndEditing.rawValue)
        
        //        if debug() {
        //            IQKeyboardManager.sharedManager().enableDebugging = true
        //        }
    }
}

fileprivate let TOKEN_KEY = "TOKEN_KEY"

extension UIApplication {
    
    class var token: String? {
        get {
            return UserDefaults.standard.object(forKey: TOKEN_KEY) as? String
        }
        set {
            let userDefaults = UserDefaults.standard
            
            if let token = newValue {
                userDefaults.set(token, forKey: TOKEN_KEY)
            } else {
                userDefaults.removeObject(forKey: TOKEN_KEY)
            }
            
            userDefaults.synchronize()
        }
    }
    
    class var isLogin: Bool {
        guard let token = token else {
            return false
        }
        return !token.isEmpty
    }
}

extension UIViewController {
    func toMain() {
        
        guard let menu = self.slideMenuController(), let left = menu.leftViewController as? MenuViewController else {
            return
        }
        
        menu.changeMainViewController(left.menuItems.first!.1, close: true)
    }
}
